<?php
namespace app\common\repositories\system;
use app\common\repositories\BaseRepository;
use app\common\dao\system\SystemCityDao as dao;
/**
 * @mixin dao
 */
class SystemCityRepository extends BaseRepository
{
    public function __construct(dao $dao)
    {
        $this->dao = $dao;
    }

    public function getFormatList( array $where)
    {
        return  formatCategory($this->dao->getAll($where)->toArray(), 'city_id','parent_id');
    }

    public function getCateList( array $where)
    {
        $column = $this->dao->getColumn($where);
        return formatCascaderData($column,'name',0,'parent_id');
    }


    public function getApiLst(array $where,$hasAll = true)
    {
        $list = formatCategory($this->dao->getAll($where)->toArray(), 'city_id','parent_id');
        return $hasAll ? $this->setApiData($list):$list;
    }

    public function setApiData($data,$city_id = '',$parent_id = '',$name = '',$level = 0)
    {

        if($level === 1)
            array_unshift($data,['city_id'=>$city_id,'name'=>$name ? ('全'.$name):'全部','merger_name'=>'全部','parent_id'=>$parent_id,'children'=>[],'level'=>$level]);
        foreach ($data as $key=>$item){
            if(isset($item['children']) && count($item['children']) > 1){
                $data[$key]['children'] = $this->setApiData($item['children'],$item['city_id'],$item['parent_id'],$item['name'],$item['level']);
            }
        }
        return $data;
    }
}